package com.csdj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.common.enums.AppHttpCodeEnum;
import com.csdj.common.utils.ResponseResult;
import com.csdj.dto.LoginDto;
import com.csdj.pojo.SyEmp;
import com.csdj.service.SyEmpService;
import com.csdj.mapper.SyEmpMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 叶涵
* @description 针对表【sy_emp】的数据库操作Service实现
* @createDate 2023-08-24 15:13:39
*/
@Service
public class SyEmpServiceImpl extends ServiceImpl<SyEmpMapper, SyEmp>
    implements SyEmpService{


    @Override
    public SyEmp login(String  empNo) {
        SyEmp emp = getOne(Wrappers.<SyEmp>lambdaQuery().eq(SyEmp::getEmpNo,empNo));
        return emp;
    }

    @Override
    public ResponseResult updatePwd(LoginDto loginDto) {
        // 校验数据
        if (loginDto.getId()==null){
            return ResponseResult.errorResult(500,"id错误，不存在");
        }
        SyEmp emp = getById(loginDto.getId());
        // 1，对比原密码
        if (!emp.getPwd().equals(loginDto.getOdlPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        emp.setPwd(loginDto.getPassword());
        if (updateById(emp)) {
            // 清空密码
            emp.setPwd("");
            return ResponseResult.okResult(emp);
        }else {
            return ResponseResult.errorResult(500,"服务器正在忙碌中...");
        }
    }

    @Override
    public List<SyEmp> getListByDeptId(int deptId) {
        LambdaQueryWrapper<SyEmp> lq =new LambdaQueryWrapper();
        lq.eq(SyEmp::getDeptID,deptId);
        List<SyEmp> syEmps = getBaseMapper().selectList(lq);
        return syEmps;
    }

}




